from django import forms

from browsertests.useragents.models import Useragent
from browsertests.results.models import Result
from browsertests.common.forms import CommonSearchForm

class ResultSearchForm(CommonSearchForm):
    useragent = forms.MultipleChoiceField(
        choices=[(u.pk, str(u)) for u in
                  Useragent.objects.select_related().filter(enabled=True)],
        required=False, widget=forms.SelectMultiple(attrs={"size":5}))
    SUCCEEDED_CHOICE = (
        ('---', '---'),
        ('True', 'True'),
        ('False', 'False'),
    )
    succeeded = forms.ChoiceField(choices=SUCCEEDED_CHOICE, required=False)
    status = forms.MultipleChoiceField(choices=Result.STATUS_CHOICE,
                                       required=False)

class CompareUAForm(forms.Form):
    ua_choices = [(u.pk, str(u)) for u in Useragent.objects.filter(enabled=True)
                                                   .select_related()]
    from_ua = forms.ChoiceField(choices=ua_choices, label="From User Agent")
    to_ua = forms.ChoiceField(choices=ua_choices, label="To User Agent")

    def clean(self):
        cleaned_data = self.cleaned_data
        if not "from_ua" in cleaned_data or not "to_ua" in cleaned_data:
            raise forms.ValidationError("Missing parameters")            
        if cleaned_data["from_ua"] == cleaned_data["to_ua"]:
            raise forms.ValidationError("You must select different useragents")
        return cleaned_data
